<template>
	<view class="notice">
		<view class="list" v-for="(item,index) of notices" :key="index"
		 @click="goPage(item.href,item.id,item.status,item.read)">
			<text class="iconfont" 
			:class="[item.status?'icon-new-article':'icon-new-notice',item.read?'radius':'']"></text>
			<view class="content">
				<view class="title">
					<text>{{item.title}}</text>
				</view>
				<view class="brief">
					<text>{{item.brief}}</text>
				</view>
				<view class="time">
					<text>{{formatDate(item.create_time*1000)}}</text>
				</view>
			</view>
		</view>
		<block-loading :status="status" :len="notices.length"></block-loading>
	</view>
</template>

<script>
	import {formatDate} from "@/utils/util.js"
	import {
		getNotices
	} from '@/api/public.js'
	export default {
		name: "notice",
		data() {
			return {
				notices: [],
				status: "loading",
				page: 1,
				redId:[]
			}
		},
		onShow() {
			this.page = 1;
			this.notices = [];
			this.getNoticeLists();
			if(uni.getStorageSync("redId")){
				this.redId = JSON.parse(uni.getStorageSync("redId"))
			}
		},
		//上拉加载
		onReachBottom() {
			this.getNoticeLists()
		},
		methods: {
			goPage(url,id,status,read){
				if(read){
					let bool = this.uniq(id);
					if(!bool){
						this.redId.push(id);
						uni.setStorageSync("redId",JSON.stringify(this.redId))
						for(let item of this.notices){
							if(item.id === id){
								item.read = false
							}
						}
					}
				}
				if(status){
					if(url){
						uni.navigateTo({
							url:"/pages/link/link?url="+url
						})
					}else{
						uni.navigateTo({
							url:'/pages/public/notice-detail/notice-detail?id='+id
						})
					}
				}else{
					uni.navigateTo({
						url:url
					})
				}
				
			},
			formatDate(value){
				return formatDate(value)
			},
			uniq(id){
				return this.redId.some((item)=>{
					return item == id
				})
			},
			async getNoticeLists() {
				let res = await getNotices({
					page: this.page,
					size: 15
				});
				if (this.page === 1 && res.data.length === 0) {
					this.status = "nodata"
					return
				}
				if (this.page === 1 && res.data.length > 0 && res.data.length < 20) {
					this.status = "nomore"
				}
				if (res.data.length === 0) {
					this.status = "nomore"
					return;
				}
				this.page++;
				res.data.forEach((item)=>{
					let bool = this.uniq(item.id)
					if(bool){
						item.read = false
					}else{
						if(item.create_time < parseInt(Date.now()/1000)-86400*3){
							item.read = false
						}else{
							item.read = true
						}
					}
					
				})
				this.notices = this.notices.concat(res.data);
			}
		},
		watch:{
			notices(newValue){
				let arr = newValue.filter(item=>{return item.read})
				if(arr.length){
					uni.setStorageSync("redRdius",true)
				}else{
					uni.setStorageSync("redRdius",false)
				}
			}
		}
	}
</script>

<style scoped lang="less" src="./notice.less">
</style>
